-- msg_step_replay_record
-- Created by panyl Mar/03/2016
-- 重播迷宫

return function(lpc)
    -- 不是在播放中，应该不用做这个判断
    -- if not StatusM.check(ME.user, "PLAYING_RECORD") then
    --     return;
    -- end

    local toIndex  = lpc.index;
    local toAction = lpc.action;

    repeat
        local action = DungeonLogM.stepPlay();

        if not action then
            -- 结束了？
            break;
        end

        if action.cmd == "check_boss_die" then
            break;
        end

        if ME.user.actionProgress >= toIndex and action.cmd == toAction.cmd then
            break;
        end
    until false;

    local extra = lpc.extra or {};

    -- 检查随机数
    local ret = {};

    local cursor = ME.user.dbase:query("randomCursor", {});

    ret["random_cursor"] = {};
    for _, id in pairs(table.append(table.keys(cursor), table.keys(extra.random_cursor))) do
        if extra.random_cursor[id] ~= cursor[id] then
            local str = ">>>>>>>>>>>>>>>>>> 随机数异常: " .. id .. " 服务器: " .. tostring(extra.random_cursor[id]) .. " 客户端: " .. tostring(cursor[id]);
            print(str);

            table.insert(ret["random_cursor"], str);
        end
    end

    ret["attrib"] = {};
    for attrib, value in pairs(extra.attrib) do
        if value ~= ME.user:queryAttrib(attrib) then
            local str = ">>>>>>>>>>>>>>>>>> 属性不匹配：" .. attrib .. " 服务器: " .. value .. " 客户端: " .. ME.user:queryAttrib(attrib);
            print(str);

            table.insert(ret["attrib"], str);
        end
    end

    ret["base_attrib"] = {};
    for attrib, value in pairs(extra.base_attrib) do
        if value ~= ME.user.dbase:query(attrib, 0) then
            local str = ">>>>>>>>>>>>>>>>>> 基础属性不匹配：" .. attrib .. " 服务器: " .. value .. " 客户端: " .. ME.user.dbase:query(attrib, 0);
            print(str);

            table.insert(ret["base_attrib"], str);
        end
    end

    local gs_summon = extra.summon or {};
    local client_summon = ME.user.dbase:query("summon", {});
    local list = table.add(table.keys(gs_summon), table.keys(client_summon));

    ret["summon"] = {};
    for _, pos in pairs(list) do
        if not gs_summon or not client_summon then
            local str = ">>>>>>>>>>>>>>>>>> " .. pos .. "位置的召唤兽数据不符合";
            print(str);

            table.insert(ret["summon"], str);
        elseif gs_summon["hp"] ~= client_summon["hp"] then
            local str = ">>>>>>>>>>>>>>>>>> " .. pos .. "位置召唤兽血量不匹配，客户端：" .. client_summon["hp"] .. "服务器：" .. gs_summon["hp"];
            print(str);

            table.insert(ret["summon"], str);
        elseif gs_summon["attack"] ~= client_summon["attack"] then
            local str = ">>>>>>>>>>>>>>>>>> " .. pos .. "位置召唤兽攻击不匹配，客户端：" .. client_summon["attack"] .. "服务器：" .. gs_summon["attack"];
            print(str);

            table.insert(ret["summon"], str);
        end
    end

    local gs_equip = extra.equipments or {};
    local client_equip = ME.user.equipments or {};
    list = table.add(table.keys(gs_equip), table.keys(client_equip));

    ret["equipments"] = {};
    for _, pos in pairs(list) do
        if sizeof(gs_equip[pos]) ~= sizeof(client_equip[pos]) then
            local str = ">>>>>>>>>>>>>>>>>> " .. pos .. "位置装备不一致";
            print(str);

            table.insert(ret["equipments"], str);
        elseif sizeof(gs_equip[pos]) > 0 and gs_equip[pos][1] ~= client_equip[pos][1] then
            local str = ">>>>>>>>>>>>>>>>>> " .. pos .. "位置装备不一致，客户端：" .. client_equip[pos][1] .. "服务器：" .. gs_equip[pos][1];
            print(str);

            table.insert(ret["equipments"], str);
        end
    end

    ret["bine"] = {};
    local gs_bine = extra.bine or {};
    local client_bine = ME.user.dbase:query("bine", {});

    if ME.user.bine then
        client_bine = ME.user.bine:getSaveData();
    end

    if gs_bine["hp"] ~= client_bine["hp"] or
       gs_bine["level"] ~= client_bine["level"] or
       gs_bine["mp"] ~= client_bine["mp"] or
       sizeof(gs_bine["skills"]) > 0 and sizeof(client_bine["skills"]) > 0 and not table.equal(gs_bine["skills"], client_bine["skills"]) then
        local str = ">>>>>>>>>>>>>>>>>> " .. "客户端/服务器魔藤数据不一致"
        print(str);

        table.insert(ret["bine"], str);
    end

    -- TODO: 称号
    if not table.equal(extra.career, ME.user:getActivePet():getCareerData()) then
        local str = ">>>>>>>>>>>>>>>>>> " .. "客户端/服务器称号数据不一致";
        print(str);

        ret["career"] = { str, };
    end

    for _, key in pairs(table.keys(ret)) do
        if sizeof(ret[key]) <= 0 then
            ret[key] = nil;
        end
    end

    local v = {["cookie"] = lpc.cookie, ["result"] = to_mapping(ret), };
    Communicate.send("CMD_STEP_REPLAY_RESULT", v);
end
